package tom.client;

import com.google.gwt.core.client.GWT;
import java.util.HashMap;

import tom.client.account.FormAccBook;
import tom.client.account.FormAccount;
import tom.client.account.FormJournalMenu;
import tom.client.account.FormVch;
import tom.client.admin.FormAppForm;
import tom.client.admin.FormAppUser;
import tom.client.admin.FormCompany;
import tom.client.admin.FormItemKind;
import tom.client.admin.FormPassword;
import tom.client.admin.FormPermission;
import tom.client.admin.FormPostCode;
import tom.client.invoice.FormContact;
import tom.client.invoice.FormContactGroup;
import tom.client.invoice.FormCustomer;
import tom.client.invoice.FormInvRefund;
import tom.client.invoice.FormInvoice;
import tom.client.invoice.FormInv;
import tom.client.invoice.FormSupplier;
import tom.client.stock.FormProduct;
import tom.client.stock.FormStockAdjust;
import tom.client.stock.FormStockLoc;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TabPanel;

public class MainMenu extends TabPanel implements ClickHandler {

	public static MainMenu menu = GWT.create(MainMenu.class);

	private final HashMap<String, String> formIds = new HashMap<String, String>();

	private AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {

		@Override
		public void onSuccess(Integer result) {
			if (result == null || result == -1) {
				Window.alert("No Access Right");
			} else {
				FormLogin.currUser.setLevel(result);
			}
		//	Window.alert(Library.USER_LEVEL[result]);
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Verify Fail");
		}
	};

	private void show(Panel frm, String title) {
		MainPanel.mainPanel.show(frm, title);
	}

	@Override
	public void onClick(ClickEvent event) {
		Button btn = ((Button) event.getSource());
		String formId = formIds.get(btn.getText());
		String header = btn.getText() + " - " + btn.getTitle();
		Library.getService().verify(formId, FormLogin.currUser.getUserName(),
				null, callback);
		callAdmin(formId, header);
		if (CompanyPopup.popup.getComCode() == null
				&& formId.compareTo("020") > 0) {
			CompanyPopup.popup.show();
		}
		callSale(formId, header);
		callAccount(formId, header);
		callStock(formId, header);
	}

	private void callStock(String formId, String header) {
		if (formId.equals("040product"))
			show(FormProduct.getPanel(), header);
		else if (formId.equals("041stock"))
			show(FormStockLoc.getPanel(), header);
		else if (formId.equals("042stkadj"))
			show(FormStockAdjust.getPanel(), header);
	}

	private void callSale(String formId, String header) {
		if (formId.equals("011contact"))
			show(FormContact.getPanel(), header);
		else if (formId.equals("012addrgrp"))
			show(FormContactGroup.getPanel(), header);
		else if (formId.equals("051debter")) {
			show(FormCustomer.getPanel(), header);
			FormCustomer.getPanel().open("");
		} else if (formId.equals("022saleinvoice")) {
			show(FormInv.getPanel(), header);
			FormInv.getPanel().clearScreen();
		} else if (formId.equals("023refund"))
			show(FormInvRefund.getPanel(), header);
		else if (formId.equals("024servicesale")) {
			show(FormInv.getPanel(), header);
			FormInv.getPanel().clearScreen();
		} else if (formId.equals("025servicerefund"))
			show(FormInvRefund.getPanel(), header);
		else if (formId.equals("029invoice"))
			show(FormInvoice.getPanel(), header);
		else if (formId.equals("031supplier"))
			show(FormSupplier.getPanel(), header);
		// }
	}

	private void callAccount(String formId, String header) {
		if (formId.equals("060account"))
			show(FormAccount.getPanel(), header);
		else if (formId.equals("061accbook"))
			show(FormAccBook.getPanel(), header);
		else if (formId.equals("062journal"))
			show(FormJournalMenu.getPanel(), header);
		else if (formId.equals("066voucher"))
			show(FormVch.getPanel(), header);
		// } else if (formId.equals("022product")) {
		// show(ManageProduct.getPanel(), header);
		// } else if (formId.equals("023bill")) {
		// show(ManageBill.getPanel(), header);
		// } else if (formId.equals("024reduce")) {
		// show(Reduction.getPanel(), header);
		// }
	}

	private void callAdmin(String formId, String header) {
		if (formId.equals("000test")) {
			show(TestForm.getPanel(), header);
		} else if (formId.equals("001pswd")) {
			FormPassword.getPanel().setParams(FormLogin.frmLogin.getUser());
			show(FormPassword.getPanel(), header);
		} else if (formId.equals("002user"))
			show(FormAppUser.getPanel(), header);
		else if (formId.equals("003company"))
			show(FormCompany.getPanel(), header);
		else if (formId.equals("004program"))
			show(FormAppForm.getPanel(), header);
		else if (formId.equals("005permis"))
			show(FormPermission.getPanel(), header);
		else if (formId.equals("006postcode"))
			show(FormPostCode.getPanel(), header);
		else if (formId.equals("007itemkind"))
			show(FormItemKind.getPanel(), header);

	}

	public MainMenu() {
		// super(1.5, Unit.EM);
		setTitle("เมนูหลัก");
		// this.setAnimationDuration(500);
		// setStyleName("");
		this.setPixelSize(MainPanel.WIDTH, MainPanel.HEIGHT);
	}

	public void refresh() {
		WindMill.setStatus(this.getTitle());
		Library.getService().getPermissionForm(formCallback);
	}

	private AsyncCallback<String[][]> formCallback = new AsyncCallback<String[][]>() {
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());
		}

		@Override
		public void onSuccess(String[][] result) {
			formIds.clear();
			MainMenu.this.clear();
			FlexTable tab = null;
			String modName = null;
			int i = 0;
			for (String[] rec : result) {
				String mod = rec[2];
				if (!mod.equals(modName)) {
					modName = mod;
					tab = new FlexTable();
					// tab.setPixelSize(800, 500);
					tab.setTitle(mod);
					tab.setCellSpacing(20);
					MainMenu.this.add(tab, rec[2]);
					i = 0;
				}

				Button button = new Button(rec[1]);
				formIds.put(button.getText(), rec[0]);
				button.setEnabled(!rec[4].equals("false"));
				button.setPixelSize(160, 60);
				button.setStyleName("MenuButton");
				button.setTitle(rec[3]);

				button.addClickHandler(MainMenu.this);
				tab.setWidget(i / 4, i % 4, button);
				i++;
			}
			MainMenu.this.selectTab(0);
		}
	};

}
